package com.auto.autoapp.task

import com.auto.autoapp.constants.Dy
import com.auto.autoapp.constants.LiveRoomId
import com.auto.autoapp.model.LiveMemberPrivateMsgSetting
import com.auto.autoapp.model.SceneType
import com.auto.autoapp.step.backToSearchPage
import com.auto.autoapp.step.checkLogin
import com.auto.autoapp.step.getCurrentScene
import com.auto.autoapp.step.toDyIndex
import com.auto.autoapp.util.SpUtil
import com.auto.autoapp.util.addLog
import com.auto.autoapp.util.clickOrTap
import com.auto.autoapp.util.getNodeList
import com.ven.assists.AssistsCore
import com.ven.assists.AssistsCore.findById
import com.ven.assists.AssistsCore.setNodeText
import com.ven.assists.stepper.Step
import com.ven.assists.stepper.StepCollector
import com.ven.assists.stepper.StepImpl
import kotlinx.coroutines.delay

class LiveMemberPMTask : StepImpl() {
    private var setting: LiveMemberPrivateMsgSetting = LiveMemberPrivateMsgSetting()
    private var currentLiveIndex = 0
    private var collectCount = 20

    //处理过的内容
    private var readList = mutableListOf<String>()
    private var liveRoomList = mutableListOf<String>()
    override fun onImpl(collector: StepCollector) {
        setting = SpUtil.getLivePMSetting()
        liveRoomList = setting.liveList.toMutableList()
        collector.next(1) {
            return@next toDyIndex()
        }.next(2) {
            val loginResult = checkLogin()
            if (loginResult.isSuccess) {
                return@next Step.get(3)
            }
            return@next Step.none
        }
            //点击首页搜索，跳转搜索页面
            .next(3) {
                val searchNodeResult = Dy.ID_INDEX_SEARCH_NODE.clickOrTap()
                if (searchNodeResult.isFailure) {
                    addLog("未找到搜索入口，请重试")
                    AssistsCore.back()
                    delay(1000)
                    return@next Step.repeat
                }
                return@next Step.get(4)
            }.next(4) {
                val editText = AssistsCore.findById(Dy.ID_SEARCH_INPUT).firstOrNull()
                collectCount = 0
                readList.clear()
                if (currentLiveIndex >= setting.liveList.size) {
                    addLog("视频错误，将重新开始")
                    currentLiveIndex = 0
                    return@next Step.repeat
                }
                val currentVideo = setting.liveList[currentLiveIndex]

                addLog("进入搜索，关键词：${currentVideo}")
                delay(1500)
                if (editText != null) {
                    editText.setNodeText(currentVideo)
                    delay(2000)
                    Dy.SEARCH_BTN.clickOrTap()
                    addLog("已执行搜索直播间：$currentVideo")
                    delay(3000)

                    addLog("scene:${getCurrentScene().getOrNull()}")
                    if (getCurrentScene().getOrNull() != SceneType.LIVE_ROOM) {
                        addLog("无法跳转视频，请检查视频地址")
                        currentLiveIndex++
                        return@next Step.repeat
                    }
                    return@next Step.get(5)
                } else {
                    addLog("搜索输入框未找到")
                    return@next Step.get(1)
                }
            }.next(5) {

                addLog("开始观看直播：${setting.waitSeconds}秒")
                delay(setting.waitSeconds * 1000L)
                val msgListNode = LiveRoomId.MSG_RC.getNodeList().getOrNull()?.firstOrNull()
                if (msgListNode == null) {
                    addLog("未找到发言列表")
                    currentLiveIndex++
                    backToSearchPage()
                    return@next Step.get(3)
                }
                while (true) {
                    val moreMsgNode =
                        LiveRoomId.NEW_MSG_ARRIVED.getNodeList().getOrNull()?.firstOrNull()
                    if (moreMsgNode != null) {
                        addLog("点击更多信息，滑倒最新评论")
                        moreMsgNode.clickOrTap()
                        delay(2000)
                    }


                    val contentList = msgListNode.findById(LiveRoomId.MSG_NODE)
                    val contentCount = contentList.size
                    for (i in 0 until contentCount) {
                        val node = contentList[i]
                        if (node.isVisibleToUser) {
                            val content = node.text
                            if (content.contains("：")) {
                                val split = content.split("：")
                                if (split[0].contains("***")) {
                                    addLog("当前直播间已设置隐私，2s后退出")
                                    delay(2000)
                                    currentLiveIndex++
                                    backToSearchPage()
                                    return@next Step.get(4)
                                } else {

                                    if (!readList.contains(content)) {
                                        readList.add(content.toString())
                                        if (content.contains(setting.speakKeywords)) {
                                            collectCount++
                                            node.clickOrTap()
                                            delay(2000)
//                                            LiveRoomId.BOTTOM_AVATAR.getNodeList().getOrNull()
//                                                ?.firstOrNull()?.clickOrTap()
//                                            delay(2000)
//                                            AssistsCore.back()
//                                            delay(1000)
                                        }
                                    } else {
                                        continue
                                    }
                                }
                            }
                            addLog("内容：${node.text}")
                        }
                    }
                    delay(2000)
                }





                return@next Step.none
            }
    }

}